package com.peach.springbootdemo.demo.config;

import com.peach.springbootdemo.demo.filter.DemoFilter;
import com.peach.springbootdemo.demo.filter.TestFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 添加 @Configuration 注解，将自定义 Filter 加入过滤链。
 * @author peacher
 * @date 2019-07-31
 */
@Configuration
public class WebConfiguration {

    @Bean
    public FilterRegistrationBean testFilterRegistration() {

        FilterRegistrationBean<TestFilter> registration = new FilterRegistrationBean<>();
        registration.setFilter(new TestFilter());
        registration.addUrlPatterns("/*");
        registration.setName("TestFilter");
        registration.setOrder(6);
        return registration;
    }

    @Bean
    public FilterRegistrationBean demoFilterRegistration() {
        FilterRegistrationBean<DemoFilter> registration = new FilterRegistrationBean<>();
        registration.setFilter(new DemoFilter());
        registration.addUrlPatterns("/*");
        registration.setName("DemoFilter");

        //Order 值设置得低，会优先被执行。
        registration.setOrder(1);
        return registration;
    }
}
